using UnityEngine;
using UnityEngine.UI;

public class HpBar : MonoBehaviour
{
    public Image hpPro;
    public Text txtHp;
    public Transform target;
    private Camera mainCamera;

    void Start()
    {
        mainCamera = Camera.main;
    }

    /**初始化，传入跟随角色role*/
    public void init(Transform role)
    {
        target = role;
    }

    private void LateUpdate()
    {
        if (target && mainCamera)
        {
            Vector3 tarPos = mainCamera.WorldToScreenPoint(target.position);
            transform.position = tarPos;
        }
    }

    public void SetHp(int max, int hp)
    {
        if (txtHp) txtHp.text = max + "/" + hp;
        hpPro.fillAmount = (float)hp / max;
    }

    void showHp(Vector3 pos)
    {
        bool isInView = true;
        if (pos.x <= 0 || pos.y <= 0) isInView = false;

        // 根据是否在视野内决定是否显示血条
        gameObject.SetActive(isInView);
    }
}